package com.captjack.micro.task.configuration.database;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author Capt Jack
 * @date 2018/6/1
 */
@Configuration
public class DatasourceConfiguration {

    /**
     * @return 数据源
     */
    @Bean(name = "mysqlDataSource")
    DataSource mysqlDataSource(@Value(value = "${spring.datasource.hikari.jdbc-url}") String jdbcUrl,
                               @Value(value = "${spring.datasource.hikari.driver-class-name}") String driverClassName,
                               @Value(value = "${spring.datasource.hikari.username}") String username,
                               @Value(value = "${spring.datasource.hikari.password}") String password,
                               @Value(value = "${spring.datasource.hikari.read-only}") boolean readOnly) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setPassword(password);
        hikariConfig.setUsername(username);
        hikariConfig.setReadOnly(readOnly);
        return new HikariDataSource(hikariConfig);
    }

}
